﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Atentis_Connector
{
	/// <summary>
	/// Мастер управления табличными данными, получаемыми от провайдера (Atentis,..)
	/// </summary>
	class SecMaster : ISecurity
	{

		private List<WrapperSecurities> m_Securities;
		private List<WrapperIndexes> m_Indexes;
		private List<WrapperNews> m_News;
		private List<WrapperOrders> m_Orders;
		private List<WrapperOrderbook> m_Orderbook;
		private List<WrapperPositions> m_Positions;
		private List<WrapperStopOrders> m_StopOrders;
		private List<WrapperTrades> m_Trades;
		private List<WrapperTrdAcc> m_TrdAcc;
		private List<WrapperFirm_Holding_Total> m_Firm_Holding_Total;
		private List<WrapperHistory> m_History;
		private List<WrapperHistory_Daily> m_History_Daily;
		private List<WrapperBoards> m_Boards;
		private List<WrapperTesystime> m_Systime;
		private List<WrapperAll_Trades> m_All_Trades;

		private SQLServerTransport m_SqlTransport;

		private bool m_SaveInDB;
		/// <summary>
		/// Сохранять ли данные в БД
		/// </summary>
		public bool SaveInDB 
		{ 
			get { return m_SaveInDB; } 
			set 
			{ 
				m_SaveInDB = value;
				if (m_SaveInDB)
					m_SqlTransport = new SQLServerTransport();
			} 
		}

		private Dictionary<string, string> m_SecForCandlesDict;
		/// <summary>
		/// Словарь инструментов по которым строятся свечи.
		/// </summary>
		public Dictionary<string, string> SecForCandlesDict { get { return m_SecForCandlesDict; } set { m_SecForCandlesDict = value; } }

		#region Таблицы

		/// <summary>
		/// Таблица Финанасовых Инструментов
		/// </summary>
		public List<WrapperSecurities> WSerurities { get { return m_Securities; } set { m_Securities = value; } }

		/// <summary>
		/// Таблица Индексов
		/// </summary>
		public List<WrapperIndexes> WIndexes { get { return m_Indexes; } set { m_Indexes = value; } }

		/// <summary>
		/// Таблица Новостей
		/// </summary>
		public List<WrapperNews> WNews { get { return m_News; } set { m_News = value; } }

		/// <summary>
		/// Таблица Заявок
		/// </summary>
		public List<WrapperOrders> WOrders { get { return m_Orders; } set { m_Orders = value; } }

		/// <summary>
		/// Таблица Стаканов
		/// </summary>
		public List<WrapperOrderbook> WOrderbook { get { return m_Orderbook; } set { m_Orderbook = value; } }

		/// <summary>
		/// Таблица Позиций по деньгам
		/// </summary>
		public List<WrapperPositions> WPositions { get { return m_Positions; } set { m_Positions = value; } }

		/// <summary>
		/// Таблица Стоп-Заявок
		/// </summary>
		public List<WrapperStopOrders> WStopOrders { get { return m_StopOrders; } set { m_StopOrders = value; } }

		/// <summary>
		/// Таблица Сделок
		/// </summary>
		public List<WrapperTrades> WTrades { get { return m_Trades; } set { m_Trades = value; } }

		/// <summary>
		/// Таблица Счетов
		/// </summary>
		public List<WrapperTrdAcc> WtrdAcc { get { return m_TrdAcc; } set { m_TrdAcc = value; } }

		/// <summary>
		/// Таблица Позиций по бумагам
		/// </summary>
		public List<WrapperFirm_Holding_Total> WFirm_Holding_Total { get { return m_Firm_Holding_Total; } set { m_Firm_Holding_Total = value; } }

		/// <summary>
		/// Таблица Истории котировок (минутки)
		/// </summary>
		public List<WrapperHistory> WHistory { get { return m_History; } set { m_History = value; } }

		/// <summary>
		/// Таблица Истории котировок (дневки)
		/// </summary>
		public List<WrapperHistory_Daily> WHistoryDaily { get { return m_History_Daily; } set { m_History_Daily = value; } }

		/// <summary>
		/// Таблица Режимов торгов
		/// </summary>
		public List<WrapperBoards> WBoards { get { return m_Boards; } set { m_Boards = value; } }

		/// <summary>
		/// Таблица Системного времени
		/// </summary>
		public List<WrapperTesystime> WTesystime { get { return m_Systime; } set { m_Systime = value; } }

		/// <summary>
		/// Таблица Всех сделок
		/// </summary>
		public List<WrapperAll_Trades> WAllTrades { get { return m_All_Trades; } set { m_All_Trades = value; } }

		#endregion Таблицы

		/// <summary>
		/// Событие - финансовые инструменты загружены.
		/// </summary>
		public event EventHandler FILoaded;

		private void OnFILoaded(EventArgs e)
		{
			EventHandler fiLoaded = FILoaded;
			if (fiLoaded != null)
				fiLoaded(this, e);
		}

		/// <summary>
		/// Событие - таблица всех сделок загружена.
		/// </summary>
		public event EventHandler AllTrdReplied;

		private void OnAllTrdReplied(EventArgs e)
		{
			EventHandler handler = AllTrdReplied;
			if (handler != null)
				handler(this, e);
		}

		/// <summary>
		/// Событие - пришла новая сделка.
		/// </summary>
		public event EventHandler HasNewTrade;

		private void OnHasNewTrade(AtentisConnectorEventArgs e)
		{
			EventHandler hasNewTrade = HasNewTrade;
			if (hasNewTrade != null)
				hasNewTrade(this, e);
		}

		/// <summary>
		/// Событие - обновилась строка стакана.
		/// </summary>
		public event EventHandler HasNewOrderbookRow;

		private void OnHasNewOrderbookRow(EventArgs e)
		{
			EventHandler hasNewOrderbookRow = HasNewOrderbookRow;
			if (hasNewOrderbookRow != null)
				hasNewOrderbookRow(this, e);
		}

		/// <summary>
		/// Событие - исполнен ордер на покупку.
		/// </summary>
		public event EventHandler BuyOrderComplete;

		private void OnBuyOrderComplete(AtentisConnectorEventArgs e)
		{
			EventHandler handler = BuyOrderComplete;
			if (handler != null)
				handler(this, e);
		}

		/// <summary>
		/// Событие - исполнен ордер на продажу.
		/// </summary>
		public event EventHandler SellOrderComplete;

		private void OnSellOrderComplete(AtentisConnectorEventArgs e)
		{
			EventHandler handler = SellOrderComplete;
			if (handler != null)
				handler(this, e);
		}

		/// <summary>
		/// Событие - обновлена позиция по деньгам.
		/// </summary>
		public event EventHandler PositionsUpdated;

		private void OnPositionsUpdated(AtentisConnectorEventArgs e)
		{
			EventHandler handler = PositionsUpdated;
			if (handler != null)
				handler(this, e);
		}

		/// <summary>
		/// Событие - обновлена позиция по бумагам.
		/// </summary>
		public event EventHandler HoldingsUpdated;

		private void OnHoldingsUpdated(AtentisConnectorEventArgs e)
		{
			EventHandler handler = HoldingsUpdated;
			if (handler != null)
				handler(this, e);
		}

		public SecMaster(bool saveInBD)
		{
			m_Securities = new List<WrapperSecurities>();
			m_Indexes = new List<WrapperIndexes>();
			m_News = new List<WrapperNews>();
			m_Orders = new List<WrapperOrders>();
			m_Orderbook = new List<WrapperOrderbook>();
			m_Positions = new List<WrapperPositions>();
			m_StopOrders = new List<WrapperStopOrders>();
			m_Trades = new List<WrapperTrades>();
			m_TrdAcc = new List<WrapperTrdAcc>();
			m_Firm_Holding_Total = new List<WrapperFirm_Holding_Total>();
			m_History = new List<WrapperHistory>();
			m_History_Daily = new List<WrapperHistory_Daily>();
			m_Boards = new List<WrapperBoards>();
			m_Systime = new List<WrapperTesystime>();
			m_All_Trades = new List<WrapperAll_Trades>();

			this.SaveInDB = saveInBD;
			if (SaveInDB) 
			m_SqlTransport = new SQLServerTransport();

			m_SecForCandlesDict = new Dictionary<string, string>();

			SecNames = new List<string>();
			
		}

		#region Сохранение во внутреннее хранилище и БД

		private Object m_SecuritiesLocker = new Object();
		/// <summary>
		/// Обновление внутреннего списка таблицы финансовых инструментов
		/// </summary>
		/// <param name="dr">Строка таблицы</param>
		public void UpdateSecurities(WrapperSecurities newRow, long RowID, bool IsReplBegin = false, bool IsReplEnd = false)
		{
			lock(m_SecuritiesLocker)
			{
				WrapperSecurities x = null;

				if (m_Securities != null)
				{
					if (IsReplBegin) SecReplBegin();
					if (IsReplEnd) SecReplEnd("WrapperSecurities");

					if (newRow != null)
					x = m_Securities.Find(t=>t.SecCode == newRow.SecCode);

					// Если она была - обновим
					if (x != null)
					{
						m_Securities.Remove(x);
					}
					if (newRow != null)
					m_Securities.Add(newRow);
				}
				//Console.WriteLine("SECURITIES is updated!");
				
			}
		}

		private void SecReplBegin()
		{
			//m_Securities.Clear();
		}

		private void SecReplEnd(string tableName)
		{
			if (SaveInDB) m_SqlTransport.SaveData(tableName, m_Securities);
			OnFILoaded(new EventArgs());
		}

		private Object m_IndexesLocker = new Object();
		/// <summary>
		/// Обновление внутреннего списка таблицы индексов
		/// </summary>
		/// <param name="dr">Строка таблицы</param>
		public void UpdateIndexes(WrapperIndexes newRow, long RowID, bool IsReplBegin = false, bool IsReplEnd = false)
		{

			lock (m_IndexesLocker)
			{

				WrapperIndexes x = null;
				if (m_Indexes != null)
				{
					if (IsReplBegin) IndReplBegin();
					if (IsReplEnd) IndReplEnd("WrapperIndexes");

					if (newRow != null)
					x = m_Indexes.Find(t => t.IndexCode == newRow.IndexCode);

					// Если она была - обновим
					if (x != null)
					{
						m_Indexes.Remove(x);
					}

					if (newRow !=null)
					m_Indexes.Add(newRow);
				}
				//Console.WriteLine("INDEXES is updated!");

				
			}

		}

		private void IndReplBegin()
		{
			m_Indexes.Clear();
		}

		private void IndReplEnd(string tableName)
		{
			if (SaveInDB) m_SqlTransport.SaveData(tableName, m_Indexes);
		}

		private Object m_NewsLocker = new Object();
		public void UpdateNews(WrapperNews newRow, long RowID, bool IsReplBegin = false, bool IsReplEnd = false)
		{
			lock (m_NewsLocker)
			{
				WrapperNews x = null;
				if (m_News != null)
				{
					if (IsReplBegin) NewsReplBegin();
					if (IsReplEnd) NewsReplEnd("WrapperNews");

					if (newRow != null)
					x = m_News.Find(t => t.NewsNo == newRow.NewsNo);

					// Если она была - обновим
					if (x != null)
					{
						m_News.Remove(x);
					}

					if (newRow !=null)
					m_News.Add(newRow);
				}
				
			}
		}

		private void NewsReplBegin()
		{
			m_News.Clear();
		}

		private void NewsReplEnd(string tableName)
		{
			if (SaveInDB) m_SqlTransport.SaveData(tableName, m_News);
		}

		private Object m_OrdersLocker = new Object();
		public void UpdateOrders(WrapperOrders newRow, long RowID, bool IsReplBegin = false, bool IsReplEnd = false)
		{
			lock (m_OrdersLocker)
			{
				WrapperOrders x = null;
				if (m_Orders != null)
				{

					if (IsReplBegin) OrdersReplBegin();
					if (IsReplEnd) OrdersSecReplEnd("WrapperOrders");

					if (newRow != null)
					x = m_Orders.Find(t => t.SecCode == newRow.SecCode);

					// Если она была - обновим
					if (x != null)
					{
						m_Orders.Remove(x);
					}

					if (newRow !=null)
					m_Orders.Add(newRow);
				}
				
			}
		}

		private void OrdersReplBegin()
		{
			m_Orders.Clear();
		}

		private void OrdersSecReplEnd(string tableName)
		{
			if (SaveInDB) m_SqlTransport.SaveData(tableName, m_Orders);
		}

		private Object m_OrderBookLocker = new Object();
		public void UpdateOrderBook(WrapperOrderbook newRow, long RowID, bool IsReplBegin = false, bool IsReplEnd = false, string tableName = "WrapperOrderbook")
		{
			lock(m_OrderBookLocker)
			{
				WrapperOrderbook x = null;
				if (m_Orderbook != null)
				{
					
					if (IsReplBegin) OrdBookReplBegin();
					if (IsReplEnd) OrdBookReplEnd(tableName);

					if (newRow != null)
					x = m_Orderbook.Find(t => t.SecCode == newRow.SecCode);

					// Если она была - обновим
					if (x != null)
					{
						m_Orderbook.Remove(x);
					}

					if (newRow !=null)
					m_Orderbook.Add(newRow);
				}
			}

		}

		private void OrdBookReplBegin()
		{
			m_Orderbook.Clear();
		}

		private void OrdBookReplEnd(string tableName)
		{
			if (SaveInDB) m_SqlTransport.SaveData(tableName, m_Orderbook);
		}

		private Object m_PositionsLocker = new Object();
		public void UpdatePositions(WrapperPositions newRow, long RowID, bool IsReplBegin = false, bool IsReplEnd = false)
		{
			lock (m_PositionsLocker)
			{
				WrapperPositions x = null;
				if (m_Positions != null)
				{

					if (IsReplBegin) PosReplBegin();
					if (IsReplEnd) PosReplEnd("WrapperPositions");

					if (newRow != null)
					x = m_Positions.Find(t => t.AccCode == newRow.AccCode && t.CurrCode == newRow.CurrCode);

					// Если она была - обновим
					if (x != null)
					{
						m_Positions.Remove(x);
					}

					if (newRow !=null)
					m_Positions.Add(newRow);
					GenerateEvents(newRow);
				}
			}
		}

		private void GenerateEvents(WrapperPositions newRow)
		{
			if (newRow == null) return;
			var acea = new AtentisConnectorEventArgs(newRow);
			OnPositionsUpdated(acea);
		}

		private void PosReplBegin()
		{
			m_Positions.Clear();
		}

		private void PosReplEnd(string tableName)
		{
			if (SaveInDB) m_SqlTransport.SaveData(tableName, m_Positions);
		}

		private Object m_StopordersLocker = new Object();
		public void UpdateStoporders(WrapperStopOrders newRow, long RowID, bool IsReplBegin = false, bool IsReplEnd = false)
		{
			lock (m_StopordersLocker)
			{
				WrapperStopOrders x = null;
				if (m_StopOrders != null)
				{

					if (IsReplBegin) StopOrdReplBegin();
					if (IsReplEnd) StopOrdReplEnd("WrapperStopOrders");

					if (newRow != null)
					x = m_StopOrders.Find(t => t.SecCode == newRow.SecCode);

					// Если она была - обновим
					if (x != null)
					{
						m_StopOrders.Remove(x);
					}

					if (newRow !=null)
					m_StopOrders.Add(newRow);
				}
				
			}
		}

		private void StopOrdReplBegin()
		{
			m_StopOrders.Clear();
		}

		private void StopOrdReplEnd(string tableName)
		{
			if (SaveInDB) m_SqlTransport.SaveData(tableName, m_StopOrders);
		}

		private Object m_TradesLocker = new Object();
		public void UpdateTrades(WrapperTrades newRow, long RowID, bool IsReplBegin = false, bool IsReplEnd = false)
		{
			lock (m_TradesLocker)
			{
				WrapperTrades x = null;
				if (m_Trades != null)
				{

					if (IsReplBegin) TradesReplBegin();
					if (IsReplEnd) TradesReplEnd("WrapperTrades");
					/*
					 * Не нужно этого, блядь!
					if (newRow != null)
					x = m_Trades.Find(t => t.SecCode== newRow.SecCode);

					// Если она была - обновим
					if (x != null)
					{
						m_Trades.Remove(x);
					}
					*/
					if (newRow !=null)
					m_Trades.Add(newRow);

					GenerateEvents(newRow);

				}
				
			}
		}

		private void GenerateEvents(WrapperTrades newRow)
		{
			if (newRow == null) return;
			var acea = new AtentisConnectorEventArgs(newRow);
			if (newRow.BuySell == "B")
				OnBuyOrderComplete(acea);
			if (newRow.BuySell == "S")
				OnSellOrderComplete(acea);
		}

		private void TradesReplBegin()
		{
			m_Trades.Clear();
		}

		private void TradesReplEnd(string tableName)
		{
			if (SaveInDB) m_SqlTransport.SaveData(tableName, m_Trades);
		}

		private Object m_TrdaccLocker = new Object();
		public void UpdateTrdacc(WrapperTrdAcc newRow, long RowID, bool IsReplBegin = false, bool IsReplEnd = false)
		{
			lock (m_TrdaccLocker)
			{
				WrapperTrdAcc x = null;
				if (m_TrdAcc != null)
				{

					if (IsReplBegin) TrdAccReplBegin();
					if (IsReplEnd) TrdAccSecReplEnd("WrapperTrdAcc");

					if (newRow != null)
					x = m_TrdAcc.Find(t => t.BankCode == newRow.BankCode);

					// Если она была - обновим
					if (x != null)
					{
						m_TrdAcc.Remove(x);
					}

					if (newRow !=null)
					m_TrdAcc.Add(newRow);
				}
				
			}
		}

		private void TrdAccReplBegin()
		{
			m_TrdAcc.Clear();
		}

		private void TrdAccSecReplEnd(string tableName)
		{
			if (SaveInDB) m_SqlTransport.SaveData(tableName, m_TrdAcc);
		}

		private Object m_HoldingsLocker = new Object();
		public void UpdateHoldings(WrapperFirm_Holding_Total newRow, long RowID, bool IsReplBegin = false, bool IsReplEnd = false)
		{
			lock (m_HoldingsLocker)
			{
				WrapperFirm_Holding_Total x = null;
				if (m_Firm_Holding_Total != null)
				{

					if (IsReplBegin) HoldReplBegin();
					if (IsReplEnd) HoldSecReplEnd("WrapperFirm_Holding_Total");

					if (newRow != null)
					x = m_Firm_Holding_Total.Find(t => t.SecCode == newRow.SecCode);

					// Если она была - обновим
					if (x != null)
					{
						m_Firm_Holding_Total.Remove(x);
					}

					if (newRow !=null)
					m_Firm_Holding_Total.Add(newRow);
					GenerateEvents(newRow);
				}
				
			}
		}

		private void GenerateEvents(WrapperFirm_Holding_Total newRow)
		{
			if (newRow == null) return;
			var acea = new AtentisConnectorEventArgs(newRow);
			OnHoldingsUpdated(acea);
		}
		private void HoldReplBegin()
		{
			m_Firm_Holding_Total.Clear();
		}

		private void HoldSecReplEnd(string tableName)
		{
			if (SaveInDB) m_SqlTransport.SaveData(tableName, m_Firm_Holding_Total);
			//m_Firm_Holding_Total.ForEach(x => Console.WriteLine(x.SecCode + " - " + x.Ticker));
//			OnFILoaded(new EventArgs());
		}

		private Object m_HistoryLocker = new Object();
		public void UpdateHistory(WrapperHistory newRow, long RowID, bool IsReplBegin = false, bool IsReplEnd = false)
		{
			lock (m_HistoryLocker)
			{
				WrapperHistory x = null;
				if (m_History != null)
				{

					if (IsReplBegin) HistReplBegin();
					if (IsReplEnd) HostReplEnd("WrapperHistory");

					if (newRow != null)
					x = m_History.Find(t => t.Time == newRow.Time && t.Volume == newRow.Volume);

					// Если она была - обновим
					if (x != null)
					{
						m_History.Remove(x);
					}

					if (newRow !=null)
					m_History.Add(newRow);
				}
				
			}
		}

		private void HistReplBegin()
		{
			m_History.Clear();
		}

		private void HostReplEnd(string tableName)
		{
			if (SaveInDB) m_SqlTransport.SaveData(tableName, m_History);
		}

		private Object m_History_DailyLocker = new Object();
		public void UpdateHistory_Daily(WrapperHistory_Daily newRow, long RowID, bool IsReplBegin = false, bool IsReplEnd = false)
		{
			lock (m_History_DailyLocker)
			{
				WrapperHistory_Daily x = null;
				if (m_History_Daily != null)
				{

					if (IsReplBegin) HistDailyReplBegin();
					if (IsReplEnd) HistDailyReplEnd("WrapperHistory_Daily");

					if (newRow != null)
					x = m_History_Daily.Find(t => t.Time == newRow.Time && t.Volume == newRow.Volume);

					// Если она была - обновим
					if (x != null)
					{
						m_History_Daily.Remove(x);
					}

					if (newRow !=null)
					m_History_Daily.Add(newRow);
				}
				
			}
		}

		private void HistDailyReplBegin()
		{
			m_History_Daily.Clear();
		}

		private void HistDailyReplEnd(string tableName)
		{
			if (SaveInDB) m_SqlTransport.SaveData(tableName, m_History_Daily);
		}

		private Object m_BoardsLocker = new Object();
		public void UpdateBoards(WrapperBoards newRow, long RowID, bool IsReplBegin = false, bool IsReplEnd = false)
		{
			lock (m_BoardsLocker)
			{
				WrapperBoards x = null;
				if (m_Boards != null)
				{

					if (IsReplBegin) BoardsReplBegin();
					if (IsReplEnd) BoardsReplEnd("WrapperBoards");

					if (newRow != null)
					x = m_Boards.Find(t => t.BoardID == newRow.BoardID && t.BoardName == newRow.BoardName);

					// Если она была - обновим
					if (x != null)
					{
						m_Boards.Remove(x);
					}

					if (newRow !=null)
					m_Boards.Add(newRow);
				}
				
			}
		}

		private void BoardsReplBegin()
		{
			m_Boards.Clear();
		}

		private void BoardsReplEnd(string tableName)
		{
			if (SaveInDB) m_SqlTransport.SaveData(tableName, m_Boards);
		}

		private Object m_SystimeLocker = new Object();
		public void UpdateSystime(WrapperTesystime newRow, long RowID, bool IsReplBegin = false, bool IsReplEnd = false)
		{
			lock (m_SystimeLocker)
			{
				WrapperTesystime x = null;
				if (m_Systime != null)
				{

					if (IsReplBegin) TimeReplBegin();
					if (IsReplEnd) TimeReplEnd("WrapperTesystime");

					if (m_Systime.Count > RowID)
						x = m_Systime[(int)RowID];

					if (newRow != null)
					x = m_Systime.Find(t => t.Time == newRow.Time);

					// Если она была - обновим
					if (x != null)
					{
						m_Systime.Remove(x);
					}

					if (newRow !=null)
					m_Systime.Add(newRow);
				}
				
			}
		}

		private void TimeReplBegin()
		{
			m_Systime.Clear();
		}

		private void TimeReplEnd(string tableName)
		{
			if (SaveInDB) m_SqlTransport.SaveData(tableName, m_Systime);
		}

		private Object m_All_TradesLocker = new Object();
		public void UpdateAll_Trades(WrapperAll_Trades newRow, long RowID, bool IsReplBegin = false, bool IsReplEnd = false)
		{
			if (newRow == null) return;
			lock (m_All_TradesLocker)
			{
				WrapperAll_Trades x = null;
				if (m_All_Trades != null)
				{

					if (IsReplBegin) AllTrdReplBegin();
					if (IsReplEnd) AllTrdReplEnd("WrapperAll_Trades");
					/*
					if (newRow != null)
					x = m_All_Trades.Find(t => t.SecCode == newRow.SecCode && t.TradeNo == newRow.TradeNo && t.TradeTime == newRow.TradeTime);

					// Если она была - обновим
					if (x != null)
					{
						m_All_Trades.Remove(x);
					}
					
					 * Ускоряем работу в 1000 раз :)
					
					*/
					if (newRow !=null)
					m_All_Trades.Add(newRow);
					/*
					if (newRow.SecCode.Contains("SBRF-6.13"))
						if (newRow.Price >= 11000)
						{
							Console.WriteLine("!!!" + newRow.SecCode + " = " + newRow.Price + "!!!");
						}
					*/
					/*
					Тут блок добавления значений на график чисто по одному инструменту
					*/
					CheckForSignal(newRow);
					
				}
				
			}
		}

		private void CheckForSignal(WrapperAll_Trades newRow)
		{
			if (m_SecForCandlesDict.ContainsKey(newRow.SecCode) && m_SecForCandlesDict.ContainsValue(newRow.SecBoard))
			{
				
				var p = m_Securities.Find(x => x.SecCode == newRow.SecCode);
				if (p != null && p.Status != "S")
				{
					//генерируем событие о приходе новой сделки
					var acea = new AtentisConnectorEventArgs(newRow);
					OnHasNewTrade(acea);
				}
				
			}
		}

		private void AllTrdReplBegin()
		{
			//m_All_Trades.Clear();
		}

		private void AllTrdReplEnd(string tableName)
		{
			if (SaveInDB) m_SqlTransport.SaveData(tableName, m_All_Trades);
			OnAllTrdReplied(new EventArgs());
		}

		#endregion Сохранение во внутреннее хранилище и БД

		private List<string> m_SecNames;
		public List<string> SecNames { get { return m_SecNames; } set { m_SecNames = value; } }
	}
}
